package com.gege.homework2;

import com.gege.homework2.service.OrderService;
import com.gege.model.Order;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 2.（必做）按自己设计的表结构，插入 100 万订单模拟数据，测试不同方式的插入效率
 *
 * @author gechenpeng
 * @date 2021-05-20
 */
public class AddBatchDemo {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-homework2.xml");
        OrderService orderService = context.getBean(OrderService.class);

        List<Order> orderList = new ArrayList<>();
        for (int i = 0; i < 1000000; i++) {
            orderList.add(Order.builder()
                    .userId(1L)
                    .totalAmount(new BigDecimal(100))
                    .build());
        }
        long start = System.currentTimeMillis();
        orderService.addOrderInBulk(orderList);
        System.out.println("costs: " + (System.currentTimeMillis() - start) / 1000.0 + " s");
    }
}
